
/**
 * 
 * @param {object, string} data 它可以是ArrayBuffer,ArrayBufferView,Blob,DOMString等对象
 * @param {string} contentDisposition 响应头的content-disposition，用于获取文件名
 * 
 */
export function handleFileDownload (data, contentDisposition) {

  const objUrl = URL.createObjectURL(new Blob([data],{type: 'application/pdf;charset-UTF-8'}))

  // 文件名处理
  let fileName = new Date().getTime() + '.txt'
  if(typeof contentDisposition !== 'undefined') {
    fileName = decodeURI(contentDisposition.split(';')[1].split('=')[1])
  }

  // 模拟a标签事件
  const aElement = document.createElement('a')
  aElement.href = objUrl
  aElement.target = '_blank'
  aElement.setAttribute("download", fileName)
  aElement.click()
  
  window.URL.revokeObjectURL(objUrl)
}